package mango.web;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import mango.functions.FuncString;



public class SubmitToken {
    public static String saveToken(HttpServletRequest request) {
        String token = UUID.randomUUID().toString();
        request.getSession().setAttribute(SubmitToken.class.getName() + token, token);
        return token;
    }

    public static boolean validateToken(HttpServletRequest request) {
        String tokenKey = SubmitToken.class.getName() + request.getParameter("SubmitToken");
        String token = (String) request.getSession().getAttribute(tokenKey);
        if (FuncString.isBlank(token)) {
            return false;
        }
        request.getSession().removeAttribute(tokenKey);
        return true;
    }
}
